#include "include/login_client.h"
#include "include/Task.h"
#include <stdio.h>

void generatePassword(const char *password,const char *salt,char *password_md5){
   
   //1.初始化结构体
   struct crypt_data data;
   memset(&data, 0, sizeof(data));

   //2.生成setting字符串
   char setting[32]={0};
   snprintf(setting,sizeof(setting),"$1$%s",salt);

   //3.生成密文
   char *p=crypt_r(password,setting, &data);
    if(p==NULL){
        //生成密文失败
        return ;
    }
    strncpy(password_md5, p, 1023);  // 假设调用方缓冲区大小为1024
    password_md5[1023] = '\0';  // 确保字符串终止
}


//客户端注册
int userRegister(const char *username,const char *password,task_t *task,char *salt){
    //1.发送用户名
    bzero(&task->_train,sizeof(task->_train));
    task->_train._trainLen=strlen(username);
    strcpy(task->_train._data,username);
    send(task->_netfd,&task->_train,sizeof(task->_train),0);
    //2.接收服务端返回的火车
    bzero(&task->_train,sizeof(task->_train));
    recv(task->_netfd,&task->_train,sizeof(task->_train),0);
    if(task->_train._trainLen!=0){
        //数据库中存在该用户
        printf("数据库中存在该用户\n");
        return -1;
    }
    //3.发送明文
    bzero(&task->_train,sizeof(task->_train));
    task->_train._trainLen=strlen(password);
    strcpy(task->_train._data,password);
    send(task->_netfd,&task->_train,sizeof(task->_train),0);
    return 0;
}

//客户端登录
int userLogin(const char *username,const char *password,task_t *task,char *salt){
    
    //1.发送用户名
    bzero(&task->_train,sizeof(task->_train));
    task->_train._trainLen=strlen(username);
    strcpy(task->_train._data,username);
    send(task->_netfd,&task->_train,sizeof(task->_train),0);
    printf("发送用户名:%s\n",task->_train._data);
    //2.接收服务端返回的火车
    bzero(&task->_train,sizeof(task->_train));
    recv(task->_netfd,&task->_train,sizeof(task->_train),0);
    if(task->_train._trainLen==0){
        //数据库中不存在该用户
        printf("数据库中不存在该用户\n");
        return -1;
    }
    //3.接收盐值并加密
    strcpy(salt,task->_train._data);
    printf("接收盐值:%s\n",salt);
    char password_md5[1024]={0};
    generatePassword(password,salt,password_md5);
    printf("加密后的密文:%s\n",password_md5);
    //4.发送密文
    bzero(&task->_train,sizeof(task->_train));
    task->_train._trainLen=strlen(password_md5);
    strcpy(task->_train._data,password_md5);
    send(task->_netfd,&task->_train,sizeof(task->_train),0);
    //5.接收服务端返回的火车
    bzero(&task->_train,sizeof(task->_train));
    recv(task->_netfd,&task->_train,sizeof(task->_train),0);
    if(strcmp(task->_train._data,"登录失败")==0){
        //登录失败
        printf("登录失败\n");
        return -1;
    }
    if(strcmp(task->_train._data,"登录成功")==0){
        //登录成功
        printf("登录成功\n");
        return 0;
    }
    return 0;
}

//用户选择登录or注册
int login_or_register(char *username,char *password,task_t *task,char *salt){
    //选择登录or注册
    printf("请选择登录或注册,注册输入r,登录输入l\n");
    char p = '0';
    scanf("%c",&p);
    fflush(stdin);
    send(task->_netfd,&p,sizeof(p),0);

    int ret;
    if(p == 'r'){
        //输入用户名密码
        char temp_username[1024]={0};
        char temp_password[1024]={0};
        printf("请输入用户名\n");
        scanf("%s",temp_username);
        fflush(stdin);
        printf("请输入密码\n");
        scanf("%s",temp_password);
        ret = userRegister(temp_username,temp_password,task,salt);
        if(ret == 0){
            printf("注册成功\n");
            strcpy(username,temp_username);
            strcpy(password,temp_password);
            return 0;
        }else{
            printf("注册失败\n");
            return -1;
        }
    }else if(p == 'l'){
        //输入用户名密码
        char temp_username[1024]={0};
        char temp_password[1024]={0};
        printf("请输入用户名\n");
        scanf("%s",temp_username);
        fflush(stdin);
        printf("请输入密码\n");
        scanf("%s",temp_password);
        ret = userLogin(temp_username,temp_password,task,salt);
        if(ret == 0){
            printf("登录成功\n");
            strcpy(username,temp_username);
            strcpy(password,temp_password);
            return 0;
        }else{
            printf("登录失败\n");
            return -1;
        }
    }else{
        printf("输入错误\n");
        return -1;
    }
} 

